﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace DynDns
{
    public class ChangeConfigValue
    {
        public void changeIP(string Name, string Ip)
        {
            string filename = System.AppDomain.CurrentDomain.BaseDirectory + @"web.config";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filename);
            XmlNodeList nodeList = xmldoc.DocumentElement.ChildNodes;

            for (int i = 0; i < nodeList.Count; i++) 
            {
                if (nodeList[i].Name.ToLower() == "appsettings")
                {
                    XmlNodeList node = nodeList[i].ChildNodes;
                    if (node.Count > 0)
                    {
                        foreach (XmlElement el in node)
                        {
                            if (el.Attributes["key"].InnerText == Name)
                            {
                                el.Attributes["value"].InnerText = Ip;          //write the value
                                string s = el.Attributes["value"].InnerText;    //read the value
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            xmldoc.Save(filename);
        }

        public void changeHost(string Name)
        {
            string filename = System.AppDomain.CurrentDomain.BaseDirectory + @"web.config";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filename);
            XmlNodeList nodeList = xmldoc.DocumentElement.ChildNodes;

            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].Name.ToLower() == "appsettings")
                {
                    XmlNodeList node = nodeList[i].ChildNodes;
                    if (node.Count > 0)
                    {
                        foreach (XmlElement el in node)
                        {
                            if (el.Attributes["key"].InnerText == "servername")
                            {
                                el.Attributes["value"].InnerText = Name;          //write the value
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            xmldoc.Save(filename);
        }

        public string readIP(string Name)
        {
            string filename = System.AppDomain.CurrentDomain.BaseDirectory + @"web.config";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filename);
            XmlNodeList nodeList = xmldoc.DocumentElement.ChildNodes;

            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].Name.ToLower() == "appsettings")
                {
                    XmlNodeList node = nodeList[i].ChildNodes;
                    if (node.Count > 0)
                    {
                        foreach (XmlElement el in node)
                        {
                            if (el.Attributes["key"].InnerText == Name)
                            {
                                string s = el.Attributes["value"].InnerText;    //read the value
                                return s;
                            }
                        }
                    }
                    break;
                }
            }
            return "";
        }
    }
}